﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;

using CNVP.Framework.Utils;
using CNVP.Config;

namespace CNVP.JShop.Web.Member
{
    public partial class ShoppingCar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        public void BindData()
        {
            //清空页面缓存
            Response.Buffer = true;
            Response.ExpiresAbsolute = System.DateTime.Now.AddSeconds(-1);
            Response.Expires = 0;
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pargma", "No-cache");
            string AddTr = "";
            BLL.BLLShoppingCar cart = new BLL.BLLShoppingCar();
            if (Request.Cookies["" + UIConfig.CookiesName + "ShoppingCart"] != null)
            {
                AddTr += "      <table class=\"ListTable\" id=\"tableEdit\" Width=\"100%\">";
                AddTr += "        <thead>";
                AddTr += "        <tr>";
                AddTr += "            <td>";
                AddTr += "                <input onclick=\"checkAllLine();TotalCount();\" id=\"checkedAll\" name=\"checkedAll\" type=\"checkbox\" title=\"全部选择/全部不选\" checked=\"checked\" />";
                AddTr += "                </td>";
                AddTr += "            <td>";
                AddTr += "                图片</td>";
                AddTr += "            <td>";
                AddTr += "                名称</td>";
                AddTr += "            <td>";
                AddTr += "                价格</td>";
                AddTr += "            <td>";
                AddTr += "                数量</td>";
                AddTr += "            <td>";
                AddTr += "                小计</td>";
                AddTr += "            <td>";
                AddTr += "                操作</td>";
                AddTr += "        </tr>";
                AddTr += "        </thead>";
                AddTr += "        <tbody>";
                Int32 SumAmount = 0;
                Decimal SumMoney = 0;
                IList<Model.Cust_ProductsDetail> iList = cart.GetDataFromCookies();
                if (iList!=null && iList.Count > 0)
                {
                    for (int i = 0; i < iList.Count; i++)
                    {
                        try
                        {
                            decimal price_UserLevelID = 0;
                            decimal DisCountPercent = 1;
                            decimal _Product_Price = 0;
                            HttpCookie Cookie = Request.Cookies["" + UIConfig.CookiesName + "UserName"];
                            HttpCookie Cookie_UserLevelID = Request.Cookies["" + UIConfig.CookiesName + "UserLevelID"];
                            if (Cookie != null && Cookie_UserLevelID!=null)
                            {
                                int UserLevelID = Convert.ToInt32(EncryptUtils.DecodeCookies(Cookie_UserLevelID.Value));
                                var model_UserLevel=new BLL.CNVP_UserLevel().GetSingle_CNVP_UserLevelById(UserLevelID);
                                if (model_UserLevel != null)
                                {
                                    DisCountPercent = model_UserLevel.DisCountPercent;
                                }
                                _Product_Price = Decimal.Parse(iList[i].Product_Price) * DisCountPercent;
                                price_UserLevelID = iList[i].Amount * Decimal.Parse(iList[i].Product_Price) * DisCountPercent;
                                SumMoney += price_UserLevelID;
                            }
                            else
                            {
                                _Product_Price = Decimal.Parse(iList[i].Product_Price);
                                price_UserLevelID = iList[i].Amount * Decimal.Parse(iList[i].Product_Price);
                                SumMoney += price_UserLevelID;
                            }
                            SumAmount += iList[i].Amount;

                            AddTr += "        <tr id=\"tr" + iList[i].ProID + "_" + iList[i].Product_ID + "\">";
                            AddTr += "            <td class=\"CheckBox\">";
                            AddTr += "               <input type=\"hidden\" id=\"ProID" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"ProIDS\" value=\"" + iList[i].ProID + "\" />";//产品主表ID
                            AddTr += "               <input type=\"hidden\" id=\"ProDetailID" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"ProDetailIDS\" value=\"" + iList[i].Product_ID + "\" />";//产品详细表ID
                            AddTr += "               <input id=\"cbCheck" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"cbCheck\" value=\"" + iList[i].ProID + "_" + iList[i].Product_ID + "\" type=\"checkbox\" checked=\"checked\" onchange='TotalCount()' />";
                            AddTr += "                </td>";
                            AddTr += "            <td>";
                            AddTr += "                <a href=\"../ProductDetail_" + iList[i].ProductClassID + "_" + iList[i].ProID + ".aspx\" target=\"blank\" title=\"" + iList[i].Product_Name + "\"><img alt=\"" + iList[i].Product_Name + "\" src=\"../UploadFile/Product/" + iList[i].Product_Image + "\" Height=\"80px\" Width=\"80px\" style=\"padding:10px 0px 10px 0px;\" /></a></td>";
                            AddTr += "            <td>";
                            AddTr += "                名称：<a href=\"../ProductDetail_" + iList[i].ProductClassID + "_" + iList[i].ProID + ".aspx\" target=\"blank\"  title=\"" + iList[i].Product_Name + "\">" + iList[i].Product_Name + "</a></br>编号：" + iList[i].ProductDetail_No + "</td>";
                            AddTr += "            <td>";
                            AddTr += "              <span id=\"Price" + iList[i].ProID + "_" + iList[i].Product_ID + "\">" + _Product_Price.ToString("f2") + "</span>";
                            AddTr += "            </td>";
                            AddTr += "            <td>";
                            AddTr += "		        <input type=\"text\" id=\"Num" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"Num\" value='" + iList[i].Amount + "'  maxlength=\"40\" class =\"txtAmount\" style=\"width:40px;\" onchange='ProductsOnchang(" + iList[i].ProID + "," + iList[i].Product_ID + "," + DisCountPercent + ")' />";
                            AddTr += "              <input type=\"hidden\" id=\"NumCompare" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"NumCompare\" value='" + iList[i].Amount + "' maxlength=\"40\"> ";
                            AddTr += "		        </td>";
                            AddTr += "            <td>";
                            AddTr += "              <span id=\"spantotalPrice" + iList[i].ProID + "_" + iList[i].Product_ID + "\">" + price_UserLevelID.ToString("f2") + "</span>";
                            AddTr += "              <input type=\"hidden\" id=\"totalPrice" + iList[i].ProID + "_" + iList[i].Product_ID + "\" name=\"totalPrice\" value=\"" + price_UserLevelID.ToString("f2") + "\" />";
                            AddTr += "              </td>";
                            AddTr += "            <td>";
                            AddTr += "               <input id=\"BTDelCar\" type=\"button\" value=\"删除\" onclick='Del(" + iList[i].ProID + "," + iList[i].Product_ID + ")' /></td>";
                            AddTr += "        </tr>";
                        }
                        catch//如果出错，代表不存在该产品ID
                        {
                            cart.Delete(iList[i].ProID.ToString(), iList[i].Product_ID.ToString(), iList[i].Amount.ToString());//删除
                        }
                    }

                }
                lbSumNum.Text = SumAmount.ToString();
                lbTotalMoney.Text = SumMoney.ToString("f2");
                AddTr += "        </tbody>";
                AddTr += "    </table>";
                lbShoppingCar.Text = AddTr;

            }
            else
            {
                InfoCar.Visible = false;
                AddTr += "      <table class=\"ListTable\" id=\"tableEdit\" Width=\"100%\">";
                AddTr += "        <tr>";
                AddTr += "            <td colspan=\"2\" align=\"right\" >";
                AddTr += "             <img src=\"../images/car.png\" style=\"padding:20px 0px 20px 0px;\" /></td>";
                AddTr += "            <td colspan=\"5\" align=\"left\">";
                AddTr += "             <b style=\"font-size:18px;\">您的购物车还是空的，赶紧行动吧！您可以：</b><br/>";
                AddTr += "             &nbsp;&nbsp;&nbsp;<b>马上去</b>&nbsp;&nbsp;<a href =\"../\">挑选宝贝</a> <br/> ";
                AddTr += "             &nbsp;&nbsp;&nbsp;<b>看看</b>&nbsp;&nbsp;<a href =\"OrderList.aspx\">查看买到的宝贝</a>  ";
                AddTr += "                </td>";
                AddTr += "            <td>";
                AddTr += "        </tr>";
                AddTr += "    </table>";
                lbShoppingCar.Text = AddTr;
            }

        }
    }
}